package com.bjtu.boatmq.broker.config;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

public class BrokerConfig {
    /* 配置属性 */
    private static int brokerId;                // brokerID
    private static String brokerName;          // broker名称
    private static String brokerHost;          // 本机地址
    private static int brokerPort;             // 监听接口
    private static int registerBrokerTimeoutMs;// 注册broker超时时间
    private static int numNetWorkThreads;      // 服务器用于接收来自网络的请求并向网络发送响应的线程数
    private static int numIoThreads;           // 服务器用于处理请求（可能包括磁盘I/O）的线程数
    private static int numNetRequestQueues;    // 服务器处理请求的最大并发量（线程池等待队列大小）
    private static int socketSendBufferBytes;  // socket server 使用的发送缓冲区
    private static int socketReceiveBufferBytes;// socket server 使用的接收缓冲区
    private static long socketRequestMaxBytes; // 接受请求的最大值
    private static String logDirs;             // 存储日志文件的目录列表
    private static int logRetentionHours;      // 日志文件存在的最短期限
    private static long logSegmentBytes;       // 日志段文件的最大大小，当达到此大小时，将创建一个新的日志段
    private static int logRetentionCheckIntervalMs; // 设置间隔，根据保留策略检查日志段是否可以删除
    private static String zookeeperConnect;    // zookeeper地址
    private static int zookeeperConnectionTimeoutMs;// 连接zookeeper超时时间(ms)

    public BrokerConfig(Properties properties) throws Exception{
        // 获取本机地址
        brokerHost = getLocalHost();

        // 读取properties对象，并赋值
        brokerId = Integer.parseInt(properties.getProperty("broker.id"));
        registerBrokerTimeoutMs = Integer.parseInt(properties.getProperty("register.broker.timeout.ms"));
        brokerName = properties.getProperty("broker.name");
        brokerPort = Integer.parseInt(properties.getProperty("broker.port"));
        numNetWorkThreads = Integer.parseInt(properties.getProperty("num.net.work.threads"));
        numNetRequestQueues = Integer.parseInt(properties.getProperty("num.net.request.queues"));
        numIoThreads = Integer.parseInt(properties.getProperty("num.io.threads"));
        socketSendBufferBytes = Integer.parseInt(properties.getProperty("socket.send.buffer.bytes"));
        socketReceiveBufferBytes = Integer.parseInt(properties.getProperty("socket.receive.buffer.bytes"));
        socketRequestMaxBytes = Long.parseLong(properties.getProperty("socket.request.max.bytes"));
        logDirs = properties.getProperty("log.dirs");
        logRetentionHours = Integer.parseInt(properties.getProperty("log.retention.hours"));
        logSegmentBytes = Long.parseLong(properties.getProperty("log.segment.bytes"));
        logRetentionCheckIntervalMs = Integer.parseInt(properties.getProperty("log.retention.check.interval.ms"));
        zookeeperConnect = properties.getProperty("zookeeper.connect");
        zookeeperConnectionTimeoutMs = Integer.parseInt(properties.getProperty("zookeeper.connection.timeout.ms"));
    }

    public static String getLocalHost() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            System.out.println("Failed to obtain the host name" + e);
        }
        return "DEFAULT_BROKER";
    }

    public static int getBrokerId() {
        return brokerId;
    }

    public static String getBrokerName() {
        return brokerName;
    }

    public static String getBrokerHost() {
        return brokerHost;
    }

    public static int getBrokerPort() {
        return brokerPort;
    }

    public static int getRegisterBrokerTimeoutMs() {
        return registerBrokerTimeoutMs;
    }

    public static int getNumNetWorkThreads() {
        return numNetWorkThreads;
    }

    public static int getNumNetRequestQueues() {
        return numNetRequestQueues;
    }

    public static int getNumIoThreads() {
        return numIoThreads;
    }

    public static int getSocketSendBufferBytes() {
        return socketSendBufferBytes;
    }

    public static int getSocketReceiveBufferBytes() {
        return socketReceiveBufferBytes;
    }

    public static long getSocketRequestMaxBytes() {
        return socketRequestMaxBytes;
    }

    public static String getLogDirs() {
        return logDirs;
    }

    public static int getLogRetentionHours() {
        return logRetentionHours;
    }

    public static long getLogSegmentBytes() {
        return logSegmentBytes;
    }

    public static int getLogRetentionCheckIntervalMs() {
        return logRetentionCheckIntervalMs;
    }

    public static String getZookeeperConnect() {
        return zookeeperConnect;
    }

    public static int getZookeeperConnectionTimeoutMs() {
        return zookeeperConnectionTimeoutMs;
    }
}
